package com.zhuss.timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhuss.util.JsonUtil;

@Component
@Configuration      
@EnableScheduling
public class DataCopyScheduleTask {
	
	
	Logger  LOGGER = LoggerFactory.getLogger(DataCopyScheduleTask.class);
	
	
    @Scheduled(cron = "11 13 0 * * ?")
    private void configureTasks() {
		String cmd ="sudo /home/wiseks/everydaydata.sh";
		
		Process obj = null;
		try {
			Thread.sleep(1000);
			obj = Runtime.getRuntime().exec(cmd);
			int status = obj.waitFor();
            LOGGER.error("status: " + status);
            LOGGER.error(JsonUtil.toJson(obj));
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error(JsonUtil.toJson(obj));
		}
		
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, -8);
		date = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String rmCmd = "sudo rm -rf /home/wiseks/datacopy/" + sdf.format(date);
		System.out.println(rmCmd);
		
		Process obj2 = null;
		try {
			Thread.sleep(1000);
			obj2 = Runtime.getRuntime().exec(rmCmd);
			int status = obj2.waitFor();
            LOGGER.error("status: " + status);
            LOGGER.error(JsonUtil.toJson(obj2));
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error(JsonUtil.toJson(obj2));
		}
		
    }
    
	
}
